import SvBase from './SvBase'

class Canvas extends SvBase {


  //开始各种事件的监听
  start() {

    //监听屏幕尺寸改变事件
    window.addEventListener('resize', this.resize);

    this.resize();

  }

  resize() {
    //窗口宽度
    let windowHeight = window.innerHeight;
    let windowWidth = window.innerWidth;
    //如果窗口尺寸发生变化,就广播尺寸发生变化
    if (this.windowHeight !== windowHeight || this.windowWidth !== windowWidth) {
      this.windowHeight = windowHeight;
      this.windowWidth = windowWidth;
      this.emit('change.windowSize', windowWidth, windowHeight);
    }
  }


  //停止监听
  stop() {
    window.removeEventListener('resize', this.resize)
  }

  mousedown(e) {//鼠标按下后
    this.draging = true;
    this.clientXS = e.clientX;
    this.clientYS = e.clientY;
  }

  mousemove(e) {
    //如果是鼠标按下的拖拽事件
    if (this.draging) {
      let clientXS = e.clientX;
      let clientYS = e.clientY;

      if (clientXS !== this.clientXS || clientYS !== this.clientYS) {
        this.emit('drag.move', (clientXS - this.clientXS), (clientYS - this.clientYS));
      }
    }
  }

  mouseup() {
    if (this.draging) {
      this.emit('drag.over');
      //清理key
      this.con('drag.move');
      this.draging = false;
    }
  }

  mouseleave() {
    if (this.draging) {
      this.emit('drag.over');
      this.con('drag.move');
      this.draging = false;
    }
  }

  mouseenter() {
  }


  mouseout() {
  }

}

export default Canvas;
